package IT.HXL.Controller;


import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(value = "/*")
public class Myfilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request1, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        //强转
        HttpServletRequest request = (HttpServletRequest) request1;
        String uri = request.getRequestURI();
        if (uri.contains("/login.jsp") || uri.contains("/checkCodeServlet") || uri.contains("/loginServlet") || uri.contains("/css/") || uri.contains("/js/") || uri.contains("/fonts/")) {
            chain.doFilter(request, response);
        } else {
            Object login = request.getSession().getAttribute("admin");

            if (login != null) {
                chain.doFilter(request, response);
                //System.out.println(login);

            } else {
                request.setAttribute("login_msg", "您尚未登录，请登录");
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            }
        }

        //chain.doFilter(request, response);
    }
}
